package io.yizoo.business.controller;

import com.fasterxml.jackson.databind.node.ObjectNode;
import io.yizoo.business.entity.biz_hrm_ei_basic;
import io.yizoo.business.entity.biz_pmis_rwzp;
import io.elm.core.CoreController;
import io.yizoo.core.flow.FlowType;
import io.yizoo.system.entity.fw_flowins;
import io.yizoo.system.entity.fw_flowsec_extra_task;
import io.yizoo.system.entity.sys_user;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/business/pmis/rwzp")
public class pmis_rwzpController extends CoreController {

    private static final String task_type = "biz_pmis_rwzp";
    private static final String status_no_begin = "未开始";
    private static final String status_running = "进行中";
    private static final String status_completed = "已完成";

    @PostMapping("/GetData")
    public Object GetData(@RequestBody String jsonStr) {
        String user_id = Ctor.getCurrentUserId();//当前登录用户id

        //查找由登录用户创建或者派发给登录用的记录
        List<?> ret = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("user_id",user_id)
                        .putFactorItem("task_type",task_type)
                        .Select("info.*,create_ei.name AS create_person_name,to_ei.name AS to_person_name,CASE WHEN info.sys_createuser=#{user_id} THEN 'yes' ELSE 'no' END AS is_creater,CASE WHEN to_ei.emp_code = check_to_ei.emp_code THEN 'yes' ELSE 'no' END AS is_to_person")
                        .From(biz_pmis_rwzp.class,"info")
                        .innerJoin(fw_flowsec_extra_task.class,"task","task.data_id = info.id AND task.type=#{task_type}","task_type")
                        .LeftJoin(sys_user.class,"create_uu","create_uu.id = info.sys_createuser")
                        .LeftJoin(biz_hrm_ei_basic.class,"create_ei","create_ei.emp_code = create_uu.emp_code")
                        .LeftJoin(biz_hrm_ei_basic.class,"to_ei","to_ei.emp_code = info.to_person")
                        .LeftJoin(sys_user.class,"check_to_uu","check_to_uu.id = #{user_id}")
                        .LeftJoin(biz_hrm_ei_basic.class,"check_to_ei","check_to_ei.emp_code = check_to_uu.emp_code")
                        .Where("and ( info.sys_createuser = #{user_id} OR to_ei.emp_code = check_to_ei.emp_code )","user_id")
                        .IsWhere("and info.content LIKE CONCAT('%', #{name}, '%')","name")
                        .IsWhere("and info.status = #{status}","status")
                        .OrderBy("info.sys_createtimestamp DESC")
                        .IsPage()
                        .GetFactor());

        return JsonResultPage(ret);
    }
    @PostMapping("/GetOneByTaskId")
    public Object GetOneByTaskId(@RequestBody String jsonStr) {
        ObjectNode data = Ctor.getObjectNode(jsonStr);
        String task_id = data.get("task_id").asText();

        //查找由登录用户创建或者派发给登录用的记录
        List<Map<String,Object>> dataList = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("task_id",task_id)
                        .putFactorItem("task_type",task_type)
                        .Select("info.*,create_ei.name AS create_person_name,to_ei.name AS to_person_name")
                        .From(biz_pmis_rwzp.class,"info")
                        .innerJoin(fw_flowsec_extra_task.class,"task","task.data_id = info.id AND task.type=#{task_type} AND task.id=#{task_id}","task_type")
                        .LeftJoin(sys_user.class,"create_uu","create_uu.id = info.sys_createuser")
                        .LeftJoin(biz_hrm_ei_basic.class,"create_ei","create_ei.emp_code = create_uu.emp_code")
                        .LeftJoin(biz_hrm_ei_basic.class,"to_ei","to_ei.emp_code = info.to_person")
                        .GetFactor());
        if(dataList.size()==0){return new HashMap<String,Object>();}

        return dataList.get(0);
    }

    @PostMapping("/Upsert")
    @Override
    public Object Upsert(@RequestBody String jsonStr) {
        ObjectNode data = Ctor.getObjectNode(jsonStr);
        String user_id = Ctor.getCurrentUserId();//当前登录用户id

        String id = Ctor.HMapper.Upsert(biz_pmis_rwzp.class, jsonStr);

        List<Map<String,Object>> checkList = Ctor.CoreMapper.USelect(
                Ctor.DMaker(jsonStr).Selector
                        .putFactorItem("task_type",task_type)
                        .Select("info.id,info.sys_createuser,info.to_person,info.status,task.id AS task_id,task.status AS task_status")
                        .From(biz_pmis_rwzp.class,"info")
                        .LeftJoin(fw_flowsec_extra_task.class,"task","task.data_id = info.id AND task.type=#{task_type}","task_type")
                        .Where("AND info.id = '" + id + "'")
                        .GetFactor());
        if(checkList.size()==0){return false;}

        Map<String,Object> check = checkList.get(0);

        biz_pmis_rwzp model = new biz_pmis_rwzp();
        model.setId(id);
        boolean needUpsertModel = false;
        if(check.get("sys_createuser")==null){
            model.setSys_createuser(user_id);
            model.setSys_createtime(new Date());
            model.setSys_createtimestamp("" + System.currentTimeMillis());
            model.setStart_time(model.getSys_createtime());
            needUpsertModel = true;
        }

        String info_status = check.get("status")==null?"nothing":check.get("status").toString();

        if(check.get("task_id")==null){
            if(check.get("to_person")!=null){
                fw_flowsec_extra_task taskModel = new fw_flowsec_extra_task();
                taskModel.setId(UUID.randomUUID().toString());
                taskModel.setSys_createuser(user_id);
                taskModel.setType(task_type);
                taskModel.setData_id(id);
                taskModel.setStatus(FlowType.node_status_running);
                taskModel.setUser_type(FlowType.node_user_type_user);
                taskModel.setEmp_code(check.get("to_person").toString());
                taskModel.setSys_createtime(new Date());
                taskModel.setSys_code_key("");

                if(data.has("content") && data.get("content")!=null){taskModel.setTitle(data.get("content").asText());}
                else{taskModel.setTitle("  ");}

                if(!info_status.equals(status_completed)){ taskModel.setStatus(FlowType.node_status_running); }
                else{ taskModel.setStatus(FlowType.node_status_completed); }

                taskModel.setStatus_time(new Date());
                model.setStatus_time(new Date());
                needUpsertModel = true;

                Ctor.HMapper.Upsert(taskModel);
            }
        }
        else{
            fw_flowsec_extra_task taskModel = new fw_flowsec_extra_task();
            taskModel.setId(check.get("task_id").toString());

            if(data.has("content") && data.get("content")!=null){taskModel.setTitle(data.get("content").asText());}
            else{taskModel.setTitle("  ");}

            if(!info_status.equals(status_completed)){ taskModel.setStatus(FlowType.node_status_running); }
            else{ taskModel.setStatus(FlowType.node_status_completed); }
            if(taskModel.getStatus()==null || !taskModel.getStatus().equals(check.get("task_status").toString())){
                taskModel.setStatus_time(new Date());
                model.setStatus_time(new Date());
                needUpsertModel = true;
            }

            Ctor.HMapper.Upsert(taskModel);
        }

        if(needUpsertModel){
            Ctor.HMapper.Upsert(model);
        }


        return true;
    }

    @PostMapping("/Delete")
    @Override
    @Transactional
    public Object Delete(@RequestBody String jsonStr) {
        ObjectNode data = Ctor.getObjectNode(jsonStr);
        String id = data.get("id").asText();

        Ctor.CoreMapper.UDelete(
                Ctor.DMaker().Deleter
                        .putFactorItem("id",id)
                        .From(biz_pmis_rwzp.class)
                        .Where("AND id = #{id} ", "id")
                        .GetFactor());
        Ctor.CoreMapper.UDelete(
                Ctor.DMaker().Deleter
                        .putFactorItem("task_type",task_type)
                        .putFactorItem("id",id)
                        .From(fw_flowsec_extra_task.class)
                        .Where(" AND type=#{task_type} AND data_id = #{id} ", "id")
                        .GetFactor());

        return true;
    }

}
